<?php

namespace app\api\controller\commons;

use app\api\controller\Common;
use think\facade\Env as getEnv;

class Lib extends Common
{
    protected $data;
    public function initialize()
    {
        $this->data = $this->request->except(['sign','accessKey','token']);
    }

    /**
     *  获取省市县列表
     */
    public function city(){
        $pid = $this->request->post('parentId');
        $info = $this->getCityList($pid);
        if(false !== $info){
            $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>$info];
        }else{
            $this->return = ['status'=>200,'code'=>'GET_CITY_ERROR','message'=>'获取失败'];
        }
        return $this->show($this->return);
    }

    /**
     *  获取省市县全部数据
     */
    public function cityAll(){
        $info = $this->getCityAllList();
        if(false !== $info){
            $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>$info];
        }else{
            $this->return = ['status'=>200,'code'=>'GET_CITY_ERROR','message'=>'获取失败'];
        }
        return $this->show($this->return);
    }

    /**
     *  获取快递公司列表
     */
    public function expressCompany(){
        $info = db('express_company')->select();
        if($info){
            $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>$info];
        }else{
            $this->return = ['status'=>200,'code'=>'GET_CITY_ERROR','message'=>'获取失败'];
        }
        return $this->show($this->return);
    }

    /**
     * 通用上传文件接口
     */
    public function files(){
        $file = $this->request->file('file');
        if($file){
            $info = $file->move(getEnv::get('root_path') . '/public/uploads');
            if($info){
                $file_path = 'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $info->getSaveName();
                $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>$file_path];
            }else{
                $this->return = ['status'=>200,'code'=>'FILE_UPLOAD_ERROR','message'=>'失败'];
            }
        }else{
            $this->return = ['status'=>200,'code'=>'FILE_SELECT_ERROR','message'=>'请选择上传文件'];
        }
        return $this->show($this->return);
    }

    public function getRefundReason(){
        //联系方式
        $refund_reason = db('config')
            ->field('id, name, value')
            ->where(['inc_type' => 'refund_reason'])->select();
        $list = [];
        foreach ($refund_reason as $val) {
            $list[$val['name']] = $val['value'];
        }
        return $this->showSuccess('退款理由', $list);
    }

    public function contact(){
        $contactArr = [
            ['id' => 1, 'title' => '联系方式一', 'inc_type' => 'contact1'],
            ['id' => 2, 'title' => '联系方式二', 'inc_type' => 'contact2'],
            ['id' => 3, 'title' => '联系方式三', 'inc_type' => 'contact3']
        ];

        $list = [];
        foreach($contactArr as $val){
            //联系方式
            $contact = db('config')
                ->field('id, name, value')
                ->where(['inc_type' => $val['inc_type']])->select();
            $tmp = [];
            foreach ($contact as $v) {
                $tmp[$v['name']] = $v['value'];
            }
            $list[] = $tmp;
        }
       
        return $this->showSuccess('联系我们', $list);

    }
}